package com.project.sumup;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.widget.TextView;

public class NumberActivity extends Activity {
	private String KEY_TOTAL = "total";
	private String KEY_DIFFICULTY = "difficulty";
	private Handler handler = new Handler();
	private TextView view;
	private Integer total = 0;
	private int startdelay = 0;
	private int delaycount = 0;
	private Difficulty diff;
	private ArrayList<Integer> randoms=new ArrayList<Integer> ();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_number);
		Intent in = getIntent();
		view = (TextView) findViewById(R.id.MyTextview);
		diff = Difficulty.valueOf(in.getStringExtra(KEY_DIFFICULTY));
		switch (diff) {
		case Easy:
			startdelay = 2000;
			delaycount = 2000;
			randoms = RandomGenerator.RandomIntegers(5, 10, 99);
			break;
		case Normal:
			startdelay = 2000;
			delaycount = 2000;
			randoms = RandomGenerator.RandomIntegers(8, 10, 99);
			break;
		case Hard:
			startdelay = 1700;
			delaycount = 1700;
			randoms = RandomGenerator.RandomIntegers(10, 10, 99);
			break;
		default:
			break;
		}

		for (int i = 0; i < randoms.size(); ++i) {
			handler.postDelayed(
					new ViewUpdater(randoms.get(i).toString(), view),
					startdelay);
			total += randoms.get(i);
			startdelay = startdelay + delaycount;
		}

		handler.postDelayed(new Runnable() {
			public void run() {
				Intent inOut = new Intent(getApplicationContext(),
						ResultActivity.class);
				inOut.putExtra(KEY_TOTAL, total.toString());
				inOut.putExtra(KEY_DIFFICULTY, diff.toString());
				startActivity(inOut);
				// MainActivity.this.finish();
			}

		}, startdelay);

	}

	private class ViewUpdater implements Runnable {
		private String mString;
		private TextView mView;

		public ViewUpdater(String string, TextView view) {
			mString = string;
			mView = view;
		}

		@Override
		public void run() {
			mView.setText(mString);
		}
	}

}
